<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class Index extends Controller
{
    //登陆
    public function login()
    {
        # code...
        if (request()->isMethod('post')) {
            $data   = [
                'username' => request('username'),
                'password' => request('password'),
            ];
            $result = (new Admin)->login($data);
            if ($result == 1) {
                $msg = [
                    'code' => 1,
                    'msg'  => '登陆成功',
                    'url'  => url('admin/index')
                ];
            } else {
                $msg = [
                    'code' => 0,
                    'msg'  => $result
                ];
            }
            return $msg;
        }
        return view('admin.index.login');
    }

    //注册
    public function register()
    {
        if (request()->isMethod('post')) {
            $data   = request()->only(['username', 'password', 'confirm', 'nickname', 'email']);
            $result = (new Admin)->register($data);
            if ($result == 1) {
                $msg = [
                    'code' => 1,
                    'msg'  => '注册成功',
                    'url'  => url('admin')
                ];
            } else {
                $msg = [
                    'code' => 0,
                    'msg'  => $result
                ];
            }
            return $msg;
        }
        return view('admin.index.register');
    }

    //忘记密码
    public function forget()
    {
        if (request()->isMethod('post')) {
            $data   = request()->only(['email']);
            $result = (new Admin)->getCode($data);
            if ($result == 1) {
                $msg = [
                    'code' => 1,
                    'msg'  => '验证码已发送'
                ];
            } else {
                $msg = [
                    'code' => 0,
                    'msg'  => $result
                ];
            }
            return $msg;
        }
        return view('admin.index.forget');
    }

//更新密码
    public function reset()
    {
        $data   = request()->only(['email', 'code']);
        $result = (new Admin)->reset($data);
        if ($result == 1) {
            $msg = [
                'code' => 1,
                'msg'  => '成功',
                'url'  => url('admin')
            ];
        }else{
            $msg = [
                'code' => 0,
                'msg'  => $result
            ];
        }
        return $msg;
    }
}
